﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace OpenLS.Spreadsheet.UI.Internals
{
    partial class CellTextBox
    {
        public void LineUp()
        {
            SetVerticalOffset(Math.Max(VerticalOffset - 1, 0));
        }

        public void LineDown()
        {
            SetVerticalOffset(VerticalOffset + 1);
        }

        public void LineLeft()
        {

        }

        public void LineRight()
        {

        }

        public void PageUp()
        {

        }

        public void PageDown()
        {

        }

        public void PageLeft()
        {

        }

        public void PageRight()
        {

        }

        public void MouseWheelUp()
        {

        }

        public void MouseWheelDown()
        {

        }

        public void MouseWheelLeft()
        {

        }

        public void MouseWheelRight()
        {

        }

        public void SetHorizontalOffset(double offset)
        {
            _scrollData.OffsetX = (int) offset;
            InvalidateVisual();
        }

        public void SetVerticalOffset(double offset)
        {
            int o = (int) offset;
            if (offset == _scrollData.OffsetY)
                return;
            int y = _scrollData.OffsetY;
            if (y < o)
                y = Math.Min(o, Enumerable.Count(getLines(ActualWidth)) - (int)this._scrollData.Viewport.Height);
            else
            {
                y = o;
            }
            _scrollData.OffsetY = y;
            InvalidateVisual();
        }

        public Rect MakeVisible(Visual visual, Rect rectangle)
        {
            return rectangle;
            //\\todo?
        }


        public bool CanVerticallyScroll
        {
            get { return _scrollData.AllowVertical; }
            set
            {
                _scrollData.AllowVertical = value;
                InvalidateMeasure();
            }
        }

        public bool CanHorizontallyScroll
        {
            get { return _scrollData.AllowHorizontal; }
            set
            {
                _scrollData.AllowHorizontal = value;
                InvalidateMeasure();
            }
        }

        public double ExtentWidth
        {
            get { return _scrollData.Extent.Width; }
        }

        public double ExtentHeight
        {
            get { return _scrollData.Extent.Height; }
        }

        public double ViewportWidth
        {
            get { return _scrollData.Viewport.Width; }
        }

        public double ViewportHeight
        {
            get { return _scrollData.Viewport.Height; }
        }

        public double HorizontalOffset
        {
            get { return _scrollData.OffsetX; }
        }

        public double VerticalOffset
        {
            get { return _scrollData.OffsetY; }
        }

        public ScrollViewer ScrollOwner
        {
            get { return _scrollData.ScrollOwner; }
            set { _scrollData.ScrollOwner = value; }
        }

        readonly ScrollData _scrollData = new ScrollData();
        private class ScrollData
        {
            internal bool AllowHorizontal;
            internal bool AllowVertical;
            internal Size Extent;
            internal int OffsetX;
            internal int OffsetY;
            internal ScrollViewer ScrollOwner;
            internal Size Viewport;


            public void SetSizes(Size extent, Size viewport)
            {
                bool changed = extent != Extent || viewport != Viewport;
                if (!changed)
                    return;
                Extent = extent;
                Viewport = viewport;
                if (ScrollOwner != null)
                    ScrollOwner.InvalidateScrollInfo();
            }
        }
    }
}